package cn.phub.org.controller;

import cn.phub.basic.annotation.PreAuthorize;
import cn.phub.basic.util.AjaxResult;
import cn.phub.basic.util.PageList;
import cn.phub.org.domain.Department;
import cn.phub.org.query.DepartmentQuery;
import cn.phub.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  * 部门管理接口
 *  *  写接口的三要素：
 *  *    1.请求地址是什么
 *  *    2.请求的参数是什么
 *  *    3.返回值是什么
 * 写接口，不叫写Controller
 * 部门接口类：支持CRUD，支持分页和高级查询
 * 一个接口类中包含多个接口【前后端分离中的接口，不是面向对象中的接口】
 */
@RestController
@RequestMapping("/dept")  // 相当于http://localhost:8080/dept
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;


    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/department/1
    @GetMapping("/{id}")//  department/1    或 /department/2
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }


    //获取所有对象：get
    //Get http://localhost:8080/department
    @GetMapping
    @ApiOperation(value = "查询全部对象" )
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/department + json参数departmentQuery
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }


    //删除一条：delete
    //下一个接口必须分析三个问题：
    //访问地址
    //传递什么参数
    //返回什么类型的数据
	//增删改我们只关心成功与否，如果失败了就返回一个失败的信息
    @DeleteMapping("/{id}")
    //Delete http://localhost:8080/department/1
    @ApiOperation(value = "删除一条数据" )
    @PreAuthorize(name = "删除记录",value = "department:deleteById")
    public AjaxResult removeById(@PathVariable("id")Long id){
        // 包起来,快捷键: ctr + alt + T
        try {
            departmentService.remove(id);
            return AjaxResult.me(); // 默认成功信息
        } catch (Exception e) {
            e.printStackTrace();
        // 相当于下面的  return new AjaxResult(false,"操作失败,"+e.getMessage());
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }

    /**
     * 批量删除, (restful风格接口中,批量相关的都要用这个注解: @PatchMapping("/{id}"))
     * @param ids
     * @return
     */
    @PatchMapping // 这里地址不带值
    //Delete http://localhost:8080/department/1
    @ApiOperation(value = "批量删除数据" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){ // 前端传过来的List<ids>
        // 包起来,快捷键: ctr + alt + T
        try {
            departmentService.batchRemove(ids); // ids是前端传过来的ids数组,包含了批量删除的部门对象的id.
            return AjaxResult.me(); // 默认成功信息
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }



    //添加和修改【差异在于是否有id】：put 和 post
    @PutMapping
    //Put http://localhost:8080/department + json参数department
    @ApiOperation(value = "新增或修改一条数据" )
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.add(department);
            }else{
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统忙,请稍后重试，"+e.getMessage())
                    .setSuccess(false);
        }
    }

    /**
     * 查询无限极部门树
     * @return
     */
    @GetMapping("/tree")
    @ApiOperation(value = "查询上级部门 无限极树" )
    public List<Department> queryTree(){
        return  departmentService.queryTree();
    }

}
